<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use App\Model\Enum\ChannelInfoTypeEnum;
use Dotenv\Validator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\XZXValidationException;
use App\Service\ChannelCollectionMchConnService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;
use App\Middleware\AdminJWTAuthMiddleware;

/**
 * Class ChannelCollectionMchConnController
 * 商户-通道集合
 *
 * @package App\Controller
 * @Controller(prefix="/admin/api/channelCollectionMchConn")
 * @Middleware(AdminJWTAuthMiddleware::class)
 */
class ChannelCollectionMchConnController extends AdminBaseController {

    /**
     * @Inject()
     * @var ChannelCollectionMchConnService
     */
    protected $ChannelCollectionMchConnService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 编辑
     * @PostMapping(path = "setBind")
     */
    public function setBind (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo'        => 'required',
            'payType'      => 'required',
        ]);

        $ext = $this->request->getAttribute("ext");

        $param = [
            'mchNo'        => $request->post('mchNo'),
            'payType'      => (int) $request->post('payType'),
            'collectionId' => $request->post('collectionId', null),
        ];

        $param['payType'] = ChannelInfoTypeEnum::born($param['payType'])->value();

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->ChannelCollectionMchConnService->setBind($param);
    }

    /**
     * 列表
     * @PostMapping(path = "list")
     */
    public function list (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'page'  => 'required|integer|min:1',
            'limit' => 'required|integer|min:1',
        ]);

        $param = [
            'page'  => $request->post('page'),
            'limit' => $request->post('limit'),
        ];

        $search = $request->post('search', []);

        CustomUtils::xzxAddSearch($param, $search, 'mchNo');
        CustomUtils::xzxAddSearch($param, $search, 'payType');
        CustomUtils::xzxAddSearch($param, $search, 'status');

        return $this->ChannelCollectionMchConnService->list($param);
    }

    /**
     * 列表
     * @PostMapping(path = "channelList")
     */
    public function channelList (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'page'  => 'required|integer|min:1',
            'limit' => 'required|integer|min:1',
        ]);

        $param = [
            'page'  => $request->post('page'),
            'limit' => $request->post('limit'),
        ];

        $search = $request->post('search', []);

        CustomUtils::xzxAddSearch($param, $search, 'mchNo');
        CustomUtils::xzxAddSearch($param, $search, 'payType');

        return $this->ChannelCollectionMchConnService->channelList($param);
    }
}
